package com.wsoft.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wsoft.constant.redis.RedisKeyConstant;
import com.wsoft.core.annotation.GetRedisCache;
import com.wsoft.core.annotation.InitRedisCache;
import com.wsoft.system.dto.SysMemberOrgPostRelationDTO;
import com.wsoft.system.entity.SysMemberEntity;
import com.wsoft.system.entity.SysMemberOrgPostRelationEntity;
import com.wsoft.system.mapper.SysMemberOrgPostRelationMapper;
import com.wsoft.system.query.member.MemberOrgPostQuery;
import com.wsoft.system.query.post.MemberPostQuery;
import com.wsoft.system.service.ISysMemberOrgPostRelationService;
import com.wsoft.system.service.ISysMemberService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 人员组织用户关联表 服务实现类
 * </p>
 *
 * @author juiet
 * @since 2024年09月27日 13:07:12
 */
@Service
public class SysMemberOrgPostRelationServiceImpl extends ServiceImpl<SysMemberOrgPostRelationMapper, SysMemberOrgPostRelationEntity> implements ISysMemberOrgPostRelationService {

    @Resource
    private ISysMemberService memberService;



    @Override
    @InitRedisCache(type = RedisKeyConstant.MEMBER_POST_CACHE,
            value = "memberId")
    @Transactional(rollbackFor = Exception.class)
    public void insertRelations(MemberOrgPostQuery query) {
        // 增加组织角色关系
        for (SysMemberOrgPostRelationDTO re : query.getRelations()) {
                SysMemberOrgPostRelationEntity entity = new SysMemberOrgPostRelationEntity();
                entity.setMemberId(query.getMemberId());
                entity.setOrgId(re.getOrgId());
                entity.setPostId(re.getPostId());
                save(entity);
            }
    }

    @Override
    @InitRedisCache(type = RedisKeyConstant.MEMBER_POST_CACHE,
            value = "memberId")
    @Transactional(rollbackFor = Exception.class)
    public void updateRelations(MemberOrgPostQuery query) {
        //清空岗位关系
        deleteByMemberId(query.getMemberId());
        insertRelations(query);
    }

    @Override
    @GetRedisCache(type = RedisKeyConstant.MEMBER_POST_CACHE,
                   value = RedisKeyConstant.REDIS_SELF)
    public List<SysMemberOrgPostRelationEntity> getPostListByMember(Long memberId) {
        QueryWrapper<SysMemberOrgPostRelationEntity> query = new QueryWrapper<>();
        query.lambda().eq(SysMemberOrgPostRelationEntity::getMemberId,memberId);
        return list(query);
    }

    @Override
    public PageInfo<SysMemberEntity> getMemberListByPost(MemberPostQuery query) {
        QueryWrapper<SysMemberOrgPostRelationEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda().eq(SysMemberOrgPostRelationEntity::getPostId, query.getPostId());
        List<SysMemberOrgPostRelationEntity> list = list(queryWrapper);
        if (!list.isEmpty()){
            PageHelper.startPage(query.getPageNum(), query.getPageSize());
            List<Long> ids = list.stream().map(SysMemberOrgPostRelationEntity::getMemberId).collect(Collectors.toList());
            return PageInfo.of(memberService.listByIds(ids));
        }
        return PageInfo.of(new ArrayList<>());
    }

    @Override
    public boolean deleteByMemberId(Long memberId){
        QueryWrapper<SysMemberOrgPostRelationEntity> query = new QueryWrapper<>();
        query.lambda().eq(SysMemberOrgPostRelationEntity::getMemberId, memberId);
        return remove(query);
    }
}
